





//
//  FourthDetail.m
//  房源、商铺、写字楼详情页
//
//  Created by MB__Lee on 2018/5/22.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "FourthDetail.h"
#import "NSString+CaculateLenght.h"

#import "ColorModel.h"

@interface FourthDetail()

@property(nonatomic,strong)NSMutableArray *mut;


@property(nonatomic,strong)ColorModel *colorModel;
//在label 上进行布局
@property (weak, nonatomic) IBOutlet UILabel *BottomLabel;
// 当前label 的 高度约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *BottomLabelHight;




@end

@implementation FourthDetail
-(ColorModel *)colorModel{
    if (!_colorModel) {
        _colorModel  = [[ColorModel alloc] init];
    }
    return  _colorModel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setModel:(MBFangDetailModel *)model{
    [self colorModel];
    
   [super setModel:model];
    [self caculateWithModel:model];
}
// 通过模型数据来计算cell 上label 的高度
-(void)caculateWithModel:(MBFangDetailModel *)model{
    
    NSArray *array  = model.label;
    
    if (array.count == 0) {
        //设置bottom  的高度
        return;
    }else{
        //设置bottom 的高度
        [self creatLabelAndMakeFrameWithArray:array];
    }
    
    
}

-(CGFloat)creatLabelAndMakeFrameWithArray:(NSArray *)arr{
    
    CGFloat margin = 10; //label 之间的间距
    CGFloat height = 18; // label 高度
    
//    int i = 0;
    
    CGRect lastFrame = CGRectMake(0, 0, 0, 0);
    
    for (int i = 0; i< arr.count; i++) {
        //取得上一次的x
        CGFloat x = CGRectGetMaxX(lastFrame);
        x += margin;
        
//        取得上一次的y
        CGFloat y = CGRectGetMinY(lastFrame);
        
        NSString *falseText = [NSString stringWithFormat:@" %@ ",arr[i]]; //给字符串前后加 空格
        CGFloat textWidth = [falseText widthForStringFontSize:MBFont(15)];  // 计算变化后的字符串的长度
        
        if ( x + textWidth > SCREEN_WIDTH - 2 *margin ) {
            x =  margin;
            
            y = height + y + margin;
        }
        
        UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(x, y, textWidth, height)];
        // 从数组里面去除颜色模型
        MBDetailModelC *detailModel  = self.colorModel.mut[i];
        UIColor *textColor = detailModel.textColor;
        UIColor *backColor =  detailModel.backColor;
        
        NSDictionary *dic  = @{NSFontAttributeName:MBFont(15),NSBackgroundColorAttributeName:backColor,NSForegroundColorAttributeName:textColor};
        NSAttributedString *attString  = [[NSAttributedString alloc] initWithString:arr[i] attributes:dic];
        
        
        label.attributedText = attString;
        
        [self.BottomLabel addSubview:label];
        
        lastFrame = label.frame;
        
    }
    
//
//    for (NSString *name in arr) {
//
//        //获取上个frame的 最大x，和 它当前的 original -y
//        CGFloat x = CGRectGetMaxX(lastFrame);
//        x += margin;
//
//        CGFloat y = lastFrame.origin.y;
//
//        //计算宽度--设置frame
//        CGFloat width = [name widthForStringFontSize:MBFont(15)];
//        CGFloat backwordx = x + width;
//
//        if (backwordx  > SCREEN_WIDTH - 20) {
//            x = margin;
//            y =  CGRectGetMaxY(lastFrame) + margin;
//        }
//
//        //生成lable
//        CGRect frame =  CGRectMake(x, y, width, height);
//        UILabel *litter  = [[UILabel alloc] initWithFrame:frame];
//
//
//        // 从数组里面去除颜色模型
//        MBDetailModelC *detailModel  = self.colorModel.mut[i];
//        UIColor *textColor = detailModel.textColor;
//        UIColor *backColor =  detailModel.backColor;
//
//        NSDictionary *dic  = @{NSFontAttributeName:MBFont(15),NSForegroundColorAttributeName:textColor,NSBackgroundColorAttributeName:backColor};
//        NSString *orignal = [NSString stringWithFormat:@"%@ ",arr[i]];
//        NSAttributedString *attString  = [[NSAttributedString alloc] initWithString:orignal attributes:dic];
//
//        litter.attributedText  = attString;
////        litter.adjustsFontSizeToFitWidth = YES;
//
//
//        i += 1;
//        //将frame 记录
//        lastFrame = frame;
//
//        [self addSubview:litter];
//
    
//    }
   

    return CGRectGetMaxY(lastFrame);
    
}


@end
